package Process;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Sess
 */
import java.io.*;
import java.net.*;
import java.util.Vector;
import Main.*;

public class ClientCommunicate implements Runnable {

    Socket s;
    Thread t;
    Vector PlayersList;
    InputStream is;
    BufferedReader br;
    OutputStream os;
    BufferedWriter bw;
    Hearts control;

    ClientCommunicate(Socket SVR, Vector PL,Hearts control) {
        this.control=control;
        s = SVR;
        PlayersList = PL;
        t = new Thread(this);
        System.out.println("New thread: " + t);
        t.start();
        try {
            is = s.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));

            os = s.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os));
        } catch (Exception Ex) {
        }
    }

    public void AddPlayer(Socket Soc, String PlayerName) {
        Vector PlayerData = new Vector();
        PlayerData.add(Soc);
        PlayerData.add(PlayerName);
        PlayersList.add(PlayerData);
    }

    public int CheckIn() {
        if (PlayersList.size() < 3) {
            return 1;
        } else {
            return 0;
        }
    }
    
     public int StartCheck() {
        if (PlayersList.size() == 3) {
            return 1;
        } else {
            return 0;
        }
    }

    public String BuildStartMessage(int no) {
        String SM = "START," + no + "," + control.Hostname;
        for (int i = 0; i < PlayersList.size(); i++) {
            Vector temp = new Vector();
            temp = (Vector) PlayersList.get(i);
            SM += ",";
            SM += (String) temp.get(1);
        }
        SM += ".";
        return SM;
    }
    
    public int StartGame(int no) {

        try {
            if (StartCheck() == 1) {
                System.out.println("Let the game START");


                    String receivedMessage, sentMessage;

                    sentMessage = BuildStartMessage(no);
                    System.out.print(sentMessage);
                   


                    bw.write(sentMessage);
                    bw.newLine();
                    bw.flush();

                    //receivedMessage = br.readLine();
                    //System.out.println("Received : " + receivedMessage);

                   
                
                return 1;
            } else {
                return 0;
            }

        } catch (Exception e) {
            System.out.println(e.toString());
            return 0;
        }
    }

    public void run() {
        try {
            System.out.println("Talking to client");
            System.out.println(s.getPort());

            String receivedMessage, sentMessage;

            if (CheckIn() == 1) {
                sentMessage = "WELCOME";
            } else {
                sentMessage = "FULL";
            }
            bw.write(sentMessage);
            bw.newLine();
            bw.flush();

            receivedMessage = br.readLine();
            System.out.println("Received : " + receivedMessage);
            if (receivedMessage.equalsIgnoreCase("BYE")) {
                System.out.println("Client has left !");
                s.close();
                return;
            } else {
                AddPlayer(s, receivedMessage);
            }



            //bw.close();
            //br.close();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}
